<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑收款记录</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        var receivableId = new Ext.form.Hidden({
          name: 'id'
        });

        var saleAmount = new Ext.form.NumberField({
          fieldLabel: '销售金额',
          name:"sale_amount",
          allowBlank: false,
          allowDecimals: true,
          allowNegative: false,
          value:<%=@saleAmount%>,
          readOnly:true,
          anchor: '90%'
        });

        var totalAmount = new Ext.form.NumberField({
          fieldLabel: '已收金额',
          name:"tatol_amount",
          allowBlank: false,
          allowDecimals: true,
          allowNegative: false,
          value:<%=@amount%>,
          readOnly:true,
          anchor: '90%'
        });
        
        //金额
        var recAmount = new Ext.form.NumberField({
          fieldLabel: '收款金额',
          name:"amount",
          allowBlank: false,
          allowDecimals: true,
          allowNegative: false,
          anchor: '90%'
        });

        //原始金额
        var currentRecordAmount = new Ext.form.Hidden({
          name: 'current_record_amount'
        });

        var paymentTypeStore = new Ext.data.ArrayStore({
          //fields: ['value', 'text'],
          fields: ['text'],
          data : paymentTypeArray
        });

        var paymentType = new Ext.form.ComboBox({
          fieldLabel: '收款方式',
          store: paymentTypeStore,
          valueField:'text',
          displayField:'text',
          typeAhead: true,
          mode: 'local',
          triggerAction: 'all',
          selectOnFocus:true,
          name:'payment_type',
          emptyText:'请选择收款方式...',
          allowBlank: false,
          anchor: '95%'
        });

        var actionDate = new Ext.form.DateField({
          fieldLabel: '收款日期',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          blankText: "收款日期必须选择",
          emptyText: '收款日期...',
          anchor: '90%'
        });
        
        //收款备注
        var desc = new Ext.form.TextField({
          fieldLabel: '收款备注',
          name: 'description',
          anchor: '95%'
        });
        
        var newReceivableForm = new Ext.FormPanel({
          labelWidth: 55, // label settings here cascade unless overridden
          url:'/receivables/update',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'amount'
            }, {
              name: 'action_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }, {
              name: 'payment_type',
              mapping:'type_of_payment'
            }, {
              name: 'description'
            }]),
          frame:true,
          //title: '添加用户',
          bodyStyle:'padding:5px 5px 0',
          width: 550,
          //height:300,
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [saleAmount]
                }, {
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [totalAmount]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [recAmount]
                }, {
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [paymentType]
                }, {
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [actionDate]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [desc]
                }]
            },receivableId],
          buttons: [{
              text: '提交',
              iconCls: 'icon-save',
              handler: function(){
                // 判断修改后的总价，是不是超出了销售订单的总价。
                if (totalAmount.getValue() - currentRecordAmount.getValue() + recAmount.getValue() > saleAmount.getValue()){
                   Ext.Msg.alert('提示信息','编辑后的总价，已经超出销售订单总金额！');
                   return ;
                }
                if (newReceivableForm.form.isValid()) {
                  newReceivableForm.form.submit({
                    method: 'GET',
                    success: function(result, response){
                      if (response.result.info == "成功") {
                        //添加成功后，隐藏窗口，并刷新Grid
                        editReceivableWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text: '关闭',
              iconCls: 'exit',
              handler: function(){
                editReceivableWin.destroy();
              }
            }]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        newReceivableForm.form.load({
          url: '/receivables/getReceivableById?id=<%=params[:id]%>',
          method : 'GET',
          waitMsg: '正在载入数据...',
          success: function(form, action){
            currentRecordAmount.setValue(recAmount.getValue());
          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        newReceivableForm.render('edit_receivable_form');
      })
    </script>
  </head>
  <body>
    <div id="edit_receivable_form"></div>
  </body>
</html>
